#include "gui.h"
#include "GView.h"

#include "platform.h"
#include <windows.h>

GView::GView(GForm* parent)
:GForm(parent)
,m_vThumb(18)
,m_hThumb(2*m_vThumb)
,m_vCurPos(0)
,m_hCurPos(0)
,m_vRange(100)
,m_hRange(300)
,m_hClientWidth(0)
,m_vClientHeight(0)
{
    GHForm hForm = createWindow(VIEW, this);
}

GView::~GView()
{

}

void GView::setViewScroll(ScollBar type, int pos ,int max)
{
    if (max < 0) max = 1;
	if (pos > max - 1) pos = max - 1;
	if (pos < 0) pos = 0;
    switch (type)
    {
    case VSCROLL:
        {
            m_vCurPos = (ushort)pos;
            m_vRange = (ushort)max;
        }
        break;
    case HSCROLL:
        {
            m_hCurPos = (ushort)pos;
            m_hRange = (ushort)max;
        }
        break;
    }
    setGViewScroll(this, type);
}

void GView::scrollTo(ScollBar type, int pos)
{
    int  range;
    range = m_vRange;
    if (type == HSCROLL)
        range = m_hRange;
    if (pos < 0) pos = 0;
    if (pos > range) pos = range - 1;
    ushort s = (ushort)pos;
    switch (type)
    {
    case VSCROLL:
        {
            if (m_vCurPos == s) return;
            m_vCurPos = s;
         }
         break;
     case HSCROLL:
         {
             if (m_hCurPos == s) return;
             m_hCurPos = s;
         }
         break;
    }
    scrollViewTo(this, type, pos);
}

//////////////////////////////////////////////////////////////////////////

void GView::wheelEvent( GWheelEvent& event )
{
	if (event.m_Flag != 0)
		return ;

    int pos;
    pos = m_vCurPos;
	if (event.m_Direct > 0)
	{
		pos -= 3;
	}else{
		pos += 3;
	}
    scrollTo(VSCROLL, pos);
}

void GView::sizeEvent( GSizeEvent& event ) 
{
    m_hClientWidth = (ushort)event.m_Size.w;
    m_vClientHeight = (ushort)event.m_Size.h;
    setViewScroll(VSCROLL, m_vCurPos, m_vRange);
    setViewScroll(HSCROLL, m_hCurPos, m_hRange);
}
